package com.xinyan.controller;

import com.alibaba.fastjson.JSON;
import com.xinyan.config.RedisKeyConstant;
import com.xinyan.dto.UserDTO;
import com.xinyan.pojo.SprayUsers;
import com.xinyan.service.ISprayUsersService;
import com.xinyan.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xzlf
 * @since 2024-03-19 17:57:43
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class SprayUsersController {

    @Autowired
    private ISprayUsersService sprayUsersService;


    @GetMapping("/test")
    public R test(String name) {
        return R.ok(name);
    }

    @PostMapping("/test2")
    public R test2(@RequestBody SprayUsers sprayUsers) {
        return R.ok(sprayUsers);
    }

    @PostMapping("/reg")
    public R reg(@RequestBody SprayUsers sprayUsers) {
        log.info("======接收到参数：{}", JSON.toJSONString(sprayUsers, true));
        sprayUsersService.reg(sprayUsers);
        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody SprayUsers sprayUsers) {
        UserDTO login = sprayUsersService.login(sprayUsers.getPhone(), sprayUsers.getPassword());
        log.info("登录接口返回：{}",JSON.toJSONString(login));
        return R.ok(login);
    }

    @PostMapping("/updateUserInfo")
    public R updateUserInfo(@RequestBody SprayUsers sprayUsers, HttpServletRequest request) {
        String header = request.getHeader(RedisKeyConstant.USER_RESPONSE_HEAD);
        sprayUsersService.updateUserInfo(sprayUsers, header);
        return R.ok();
    }


    @GetMapping("/getUserInfo")
    public R getUserInfo(HttpServletRequest request) {
        String header = request.getHeader(RedisKeyConstant.USER_RESPONSE_HEAD);
//        log.info("=====接收到头信息：{}========",header);
        UserDTO userInfoByUUID = sprayUsersService.findUserInfoByUUID(header);
//        log.info("=====返回信息：{}========",JSON.toJSONString(userInfoByUUID));

        return R.ok(userInfoByUUID);
    }

    @GetMapping("/logOut")
    public R logOut(HttpServletRequest request) {
        String header = request.getHeader(RedisKeyConstant.USER_RESPONSE_HEAD);
//        log.info("=====接收到头信息：{}========",header);
        UserDTO userInfoByUUID = sprayUsersService.deleteUserInfoByUUID(header);
//        log.info("=====返回信息：{}========",JSON.toJSONString(userInfoByUUID));

        return R.ok(userInfoByUUID);
    }

}
